import 'package:library_base/entities/event/dispatcher_goods_count.dart';
import 'package:library_base/entities/schedul_child_entity.dart';
import 'package:library_base/entities/schedul_entity.dart';
import 'package:library_base/event/event.dart';
import 'package:library_base/net/apis.dart';
import 'package:library_base/network/http_config.dart';
import 'package:get/get.dart';
import 'package:library_base/provider/base_list_controller.dart';
import 'package:library_base/utils/num_util.dart';
import 'package:library_base/widget/dialog/dialog_util.dart';
import 'package:library_base/widget/multi_state_widget/multi_state_widget.dart';

class ScheduledController extends BaseListController<SchedulEntity, SchedulChildEntity> {
  String stationTimeBegin = '';

  /// 实体类
  RxList<SchedulChildEntity> listDataSelected = <SchedulChildEntity>[].obs;

  Rx<bool> isSelectAll = false.obs;

  Rxn<DateTime?> currentTime = Rxn<DateTime?>();

  void Function()? callback;

  @override
  Future requestData() {
    isFirstLoading = true;
    requestGeneralListData(Apis.SCHEDUL_PAGE,
        isRefresh: false,
        method: FBResultFul.GET,
        queryParameters: {
          "pageSize": '$limit',
          "pageNo": '${getPage(false)}',
          "stationTimeBegin": stationTimeBegin,
          "stationTimeEnd": stationTimeBegin,
        });
    return super.requestData();
  }

  @override
  List<SchedulChildEntity> transformObject(SchedulEntity data, bool isRefresh) {
    if (isRefresh || isFirstLoading) {
      listDataSelected.clear();
      Event.eventBus.fire(DispatcherGoodsCount(0, data.total));
    }
    for (var element in listDataSelected.value) {
      for (var record in data.list) {
        if (record == element) {
          record.isSelected = true;
        }
      }
    }
    if (!isRefresh && data.list.isNotEmpty) {
      isSelectAll.value = false;
    }
    return data.list;
  }

  @override
  Future onRefreshCallback() async {
    requestGeneralListData(Apis.SCHEDUL_PAGE,
        isRefresh: true,
        method: FBResultFul.GET,
        queryParameters: {
          "pageSize": '$limit',
          "pageNo": '${getPage(true)}',
          "stationTimeBegin": stationTimeBegin,
          "stationTimeEnd": stationTimeBegin,
        });
  }

  @override
  Future onLoadCallback() async {
    requestGeneralListData(Apis.SCHEDUL_PAGE,
        isRefresh: false,
        method: FBResultFul.GET,
        queryParameters: {
          "pageSize": '$limit',
          "pageNo": '${getPage(false)}',
          "stationTimeBegin": stationTimeBegin,
          "stationTimeEnd": stationTimeBegin,
        });
  }

  void onSearch(String action) {}

  void backToList() {
    isFirstLoading = true;
    multiControllerOut.viewStateChange(MultiStateStatus.loading);
    requestGeneralListData(Apis.SCHEDUL_PAGE,
        isRefresh: false,
        method: FBResultFul.GET,
        queryParameters: {
          "pageSize": '$limit',
          "pageNo": '${getPage(false)}',
          "stationTimeBegin": stationTimeBegin,
          "stationTimeEnd": stationTimeBegin,
        });
  }

  void reset() {
    stationTimeBegin = '';
    listDataSelected.clear();
    isFirstLoading = true;
    multiControllerOut.viewStateChange(MultiStateStatus.loading);
    baseState?.setStateCallBack?.call();
    requestGeneralListData(Apis.SCHEDUL_PAGE,
        isRefresh: false,
        method: FBResultFul.GET,
        params: {},
        queryParameters: {
          "pageSize": '$limit',
          "pageNo": '${getPage(false)}',
          "stationTimeBegin": stationTimeBegin,
          "stationTimeEnd": stationTimeBegin,
        });
  }

  void tryReLoad() {
    listDataSelected.clear();
    isFirstLoading = true;
    multiControllerOut.viewStateChange(MultiStateStatus.loading);
    baseState?.setStateCallBack?.call();
    requestGeneralListData(Apis.SCHEDUL_PAGE,
        isRefresh: false,
        method: FBResultFul.GET,
        params: {},
        queryParameters: {
          "pageSize": '$limit',
          "pageNo": '${getPage(false)}',
          "stationTimeBegin": stationTimeBegin,
          "stationTimeEnd": stationTimeBegin,
        });
  }

  void onItemClick(SchedulChildEntity itemData) {
    itemData.isSelected = !itemData.isSelected;
    if (!listDataSelected.value.contains(itemData)) {
      listDataSelected.value.add(itemData);
    } else {
      listDataSelected.value.remove(itemData);
    }
    isSelectAll.value = listDataSelected.length == listData.length;
    baseState!.setState(() {});
  }

  void onItemChildClick(SchedulChildEntity itemData) {
    itemData.isSelected = !itemData.isSelected;
    if (!listDataSelected.value.contains(itemData)) {
      listDataSelected.value.add(itemData);
    } else {
      listDataSelected.value.remove(itemData);
    }
    isSelectAll.value = listDataSelected.length == listData.length;
    baseState!.setState(() {});
  }

  void platoon() {
    var totalCount = 0.0;
    var distributionDetailReqList = [];
    for (var element in listDataSelected.value) {
      totalCount = NumUtil.add(totalCount, element.count);
      distributionDetailReqList.add({'orderId': element.orderId});
    }
    var map = <String, dynamic>{};
    map['totalCount'] = totalCount;
    map['currentTime'] = currentTime.value;
    map['distributionDetailReqList'] = distributionDetailReqList;
    map['callback'] = callback;
    DialogUtil.showCreateDeliveryNoteDialog(arguments: map);
  }

  void selectedAll() {
    if (listDataSelected.length != listData.length) {
      for (var element in listData.value) {
        element.isSelected = true;
      }
      listDataSelected.value.clear();
      listDataSelected.addAll(listData.value);
      isSelectAll.value = listDataSelected.length == listData.length;
    } else {
      for (var element in listData.value) {
        element.isSelected = false;
      }
      listDataSelected.value.clear();
      isSelectAll.value = listDataSelected.length == listData.length;
    }
    baseState!.setState(() {});
  }
}
